<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="80px">
      <el-form-item label="服务名称" prop="serviceName">
        <el-input v-model="formData.serviceName" placeholder="请输入服务名称" />
      </el-form-item>
      <el-form-item label="服务编号" prop="serviceCode">
        <el-input v-model="formData.serviceCode" placeholder="请输入服务编号" />
      </el-form-item>
      <el-form-item label="接口名" prop="providerServiceInterfaceName">
        <el-input v-model="formData.providerServiceInterfaceName" placeholder="请输入接口名" />
      </el-form-item>
      <el-form-item label="方法名" prop="providerServiceMethodName">
        <el-input v-model="formData.providerServiceMethodName" placeholder="请输入方法名" />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="formData.type" clearable placeholder="请选择类型">
          <el-option
v-for="dict in getIntDictOptions(DICT_TYPE.OPENAPI_TYPE)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="appId" prop="appId">
        <el-input v-model="formData.appId" placeholder="请输入appId" />
      </el-form-item>
      <el-form-item label="appSecret" prop="appSecret">
        <el-input v-model="formData.appSecret" placeholder="请输入appSecret" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="formData.status" clearable placeholder="请选择状态">
          <el-option
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { CommonStatusEnum } from '@/utils/constants'
import { OpenApiConnectionConfigApi, OpenApiConnectionConfigVO } from '@/api/system/openapi';

defineOptions({ name: 'SystemRoleForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  status: CommonStatusEnum.ENABLE,
  serviceCode: undefined,// 调用服务code
  serviceName: undefined, // 调用服务名称
  providerServiceInterfaceName: undefined,// 提供服务的接口名
  providerServiceMethodName: undefined, // 提供服务的方法名
  providerServiceReturnClassType: undefined,// 返回类型CLASS名
  parameterDemo: undefined,// 参数demo
  type: undefined,// 0：内部接口 1：外部HTTP接口 2：H5页面 3：小程序 4：APP
  appId: undefined,// 外部调用的appId
  appSecret: undefined, // 外部调用的appSecret
})
const formRules = reactive({
  serviceName: [{ required: true, message: '服务名称不能为空', trigger: 'blur' }],
  serviceCode: [{ required: true, message: '服务编码不能为空', trigger: 'change' }],
  providerServiceInterfaceName: [{ required: true, message: '服务接口名不能为空', trigger: 'change' }],
  providerServiceMethodName: [{ required: true, message: '服务方法名不能为空', trigger: 'change' }],
  type: [{ required: true, message: '服务类型不能为空', trigger: 'change' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OpenApiConnectionConfigApi.getOpenApiConnectionConfig(id)
    } finally {
      formLoading.value = false
    }
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    serviceCode: undefined,// 调用服务code
    serviceName: undefined, // 调用服务名称
    providerServiceInterfaceName: undefined,// 提供服务的接口名
    providerServiceMethodName: undefined, // 提供服务的方法名
    providerServiceReturnClassType: undefined,// 返回类型CLASS名
    parameterDemo: undefined,// 参数demo
    type: undefined,// 0：内部接口 1：外部HTTP接口 2：H5页面 3：小程序 4：APP
    appId: undefined,// 外部调用的appId
    appSecret: undefined, // 外部调用的appSecret
    status: CommonStatusEnum.ENABLE,
  }
  formRef.value?.resetFields()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as OpenApiConnectionConfigVO
    if (formType.value === 'create') {
      await OpenApiConnectionConfigApi.createOpenApiConnectionConfig(data)
      message.success(t('common.createSuccess'))
    } else {
      await OpenApiConnectionConfigApi.updateOpenApiConnectionConfig(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
